#include "additems.h"
#include "qaction.h"
#include "qgraphicsitem.h"
#include "mainwindow.h"
#include "graphicsscene.h"
#include "graphicstextitem.h"
#include "graphicsrectitem.h"
#include "graphicsview.h"
#include "dragdrop.h"
#include "graphicslineitem.h"

//AddItems g_AddItems;
AddItems *gAddItems = NULL;//&g_AddItems;
AddItems::AddItems(QObject *parent)
	: QObject(parent)
{
	mDrawRect = false;
}

AddItems::~AddItems()
{

}


void AddItems::addItemEvent()//QObject * sender)
{
	QAction * action = qobject_cast<QAction*>(sender());
	if( !action )	return;

	QGraphicsItem *item = createItem(action);

	if(item != 0 )
	{
		gSence->addItem(item);

//		gMainWindow->connectItem((QObject*)(item));
		gMainWindow->setDirty(true);
	}
}
QGraphicsItem * AddItems::createItem(QAction * action)
{
	QGraphicsItem *item = 0;
	//int type = action->data().toInt();
	QString str = action->data().toString();
	if( str == tr("addtextitem")) {
		item = this->createTextItem();
	}
	else if( str == tr("addrectitem")) {
		item = this->createRectItem();
	}
	else if( str == tr("DrawRectByMouse"))
	{
		item = createRectItemByDraw();
	}
	else if( str == tr("AddLineItem"))
	{
		//item = createLineItemByDraw();
		item = createLineItemByConnect();
	}

	if (item)
	{
		//item->setPos(100,100);
		item->setSelected(true);
	}
	return item;
}
QGraphicsItem * AddItems::createLineItemByDraw()
{
	QPointF p = gDragDrop->getPressPos();
	QPointF px = gDragDrop->getReleasePos();

	GraphicsLineItem * item = new GraphicsLineItem;
	//item->setPos(p);
	item->setLine(QLineF(p,px));
	//item->setRect(0,0,	px.x() - p.x(),	px.y() - p.y());

	return item;
}
QGraphicsItem * AddItems::createLineItemByConnect()
{
	GraphicsLineItem * item = new GraphicsLineItem;
	item->connectItem(gDragDrop->getPressItem(), gDragDrop->getReleaseItem());

	return item;
}
QGraphicsItem * AddItems::createLineItem()
{
	QGraphicsItem * item = new GraphicsLineItem;
	
	return item;
}
QGraphicsItem * AddItems::createTextItem()
{
	QGraphicsItem * item = new GraphicsTextItem;
	//item->setSelected(true);
	return item;
}
QGraphicsItem * AddItems::createRectItem()
{
	QGraphicsItem * item = new GraphicsRectItem;
	return item;
}

QGraphicsItem * AddItems::createRectItemByDraw()
{
	QPointF p1 = gDragDrop->getPressPos();
	QPointF p2 = gDragDrop->getReleasePos();

	GraphicsRectItem * item = new GraphicsRectItem;
	item->setPos(p1);

	QRectF rc = QRectF(0,0,	p2.x() - p1.x(), p2.y() - p1.y());
	item->setRect( rc );
	QRectF rc1 = item->boundingRect();

	return item;

}